<template>
  <div class="city-list" ref="cityList">
    <div class="wrapper">
      <div class="area">
        <h1 class="title border-topbottom">当前城市</h1>
        <ul class="list-btn">
          <li class="item"><div class="btn">{{ currentCity }}</div></li>
        </ul>
      </div>
      <div class="area">
        <h1 class="title border-topbottom">热门城市</h1>
        <ul class="list-btn">
          <li class="item" v-for="city in hot" :key="city.id" @click="handleCityClick(city.name)">
            <div class="btn">{{ city.name }}</div>
          </li>
        </ul>
      </div>
      <div class="area" v-for="(item, key) in cities" :key="key" :ref="key">
        <h1 class="title border-topbottom">{{ key }}</h1>
        <ul class="list-city">
          <li class="item border-bottom" v-for="subItem in item" :key="subItem.id" @click="handleCityClick(subItem.name)">{{ subItem.name }}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll';
import { mapState, mapMutations } from 'vuex';

export default {
  name: 'city-list',
  props: {
    cities: Object,
    hot: Array,
    letter: String
  },
  data() {
    return {
      scroll: null
    }
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.cityList);
  },
  watch: {
    letter() {
      if(this.letter) {
        let ele = this.$refs[this.letter][0];

        this.scroll.scrollToElement(ele);
      }
    }
  },
  methods: {
    ...mapMutations(['changeCity']),
    handleCityClick(city) {
      this.changeCity(city);
      this.$router.push('/');
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~styles/variable.styl';

.border-topbottom
  &:before, &:after
    border-color #ccc
.border-bottom 
  &:before
    border-color #ccc
.city-list
  position absolute
  top 1.58rem
  left 0
  bottom 0
  right 0
  overflow hidden
  .title 
    line-height .44rem
    background #eee
    color #666
    padding-left .2rem
    font-size .26rem
  .area
    .list-btn
      padding .1rem .6rem .1rem .1rem
      .item
        display inline-block
        width 33.33%
        .btn
          margin .1rem
          padding .1rem 0
          text-align center
          border .02rem solid #ccc
          border-radius .06rem
    .list-city
      .item
        line-height .76rem
        padding-left .2rem
</style>